package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2020/12/15
 * @description
 */
public class QueryJson1 {

    public static void main(String[] args) {
        new Query().next();
    }


    public static class Query {
        List<DowdLoadImg2.DataList> urlList = new ArrayList<>();
        int limit = 24;
        int skip;
        String url ;

        public void next() {
            if (skip < 300) {
                url = String.format("http://service.avatar.adesk.com/v1/avatar/avatar?order=hot&limit=%s&skip=%s", limit, (skip * 24));
                System.out.println(skip);
                skip++;
                getDns();
            }else {
                System.out.println("ssss " + urlList.size());
                new DowdLoadImg2().addList(urlList);
            }
        }

        public void getDns() {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .build();
            final Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                }

                @Override
                public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                    if (response.body() != null) {
                        String html = response.body().string();
                        Bean.Root bean = GsonUtil.parse(html, Bean.Root.class);
                        for (Bean.Avatar avatar : bean.getRes().avatar) {
                            urlList.add(new DowdLoadImg2.DataList(avatar.thumb));
                        }
                        next();
                    }
                }
            });
        }


        public static class Bean {
            public class User implements Serializable {
                private String name;

                private int viptime;

                private String auth;

                private int follower;

                private String avatar;

                private boolean isvip;

                private String id;

                public void setName(String name) {
                    this.name = name;
                }

                public String getName() {
                    return this.name;
                }

                public void setViptime(int viptime) {
                    this.viptime = viptime;
                }

                public int getViptime() {
                    return this.viptime;
                }

                public void setAuth(String auth) {
                    this.auth = auth;
                }

                public String getAuth() {
                    return this.auth;
                }

                public void setFollower(int follower) {
                    this.follower = follower;
                }

                public int getFollower() {
                    return this.follower;
                }

                public void setAvatar(String avatar) {
                    this.avatar = avatar;
                }

                public String getAvatar() {
                    return this.avatar;
                }

                public void setIsvip(boolean isvip) {
                    this.isvip = isvip;
                }

                public boolean getIsvip() {
                    return this.isvip;
                }

                public void setId(String id) {
                    this.id = id;
                }

                public String getId() {
                    return this.id;
                }
            }

            public class Avatar implements Serializable {
                private List<String> url;

                private int atime;

                private int ncos;

                private String thumb;

                private int rank;

                private String name;

                private List<String> tag;

                private User user;

                private int view;

                private List<String> cr;

                private int favs;

                private String id;

                private String desc;

                public void setUrl(List<String> url) {
                    this.url = url;
                }

                public List<String> getUrl() {
                    return this.url;
                }

                public void setAtime(int atime) {
                    this.atime = atime;
                }

                public int getAtime() {
                    return this.atime;
                }

                public void setNcos(int ncos) {
                    this.ncos = ncos;
                }

                public int getNcos() {
                    return this.ncos;
                }

                public void setThumb(String thumb) {
                    this.thumb = thumb;
                }

                public String getThumb() {
                    return this.thumb;
                }

                public void setRank(int rank) {
                    this.rank = rank;
                }

                public int getRank() {
                    return this.rank;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public String getName() {
                    return this.name;
                }

                public void setTag(List<String> tag) {
                    this.tag = tag;
                }

                public List<String> getTag() {
                    return this.tag;
                }

                public void setUser(User user) {
                    this.user = user;
                }

                public User getUser() {
                    return this.user;
                }

                public void setView(int view) {
                    this.view = view;
                }

                public int getView() {
                    return this.view;
                }

                public void setCr(List<String> cr) {
                    this.cr = cr;
                }

                public List<String> getCr() {
                    return this.cr;
                }

                public void setFavs(int favs) {
                    this.favs = favs;
                }

                public int getFavs() {
                    return this.favs;
                }

                public void setId(String id) {
                    this.id = id;
                }

                public String getId() {
                    return this.id;
                }

                public void setDesc(String desc) {
                    this.desc = desc;
                }

                public String getDesc() {
                    return this.desc;
                }
            }

            public class Res implements Serializable {
                private List<Avatar> avatar;

                public void setAvatar(List<Avatar> avatar) {
                    this.avatar = avatar;
                }

                public List<Avatar> getAvatar() {
                    return this.avatar;
                }
            }


            public class Root implements Serializable {
                private String msg;

                private Res res;

                private int code;

                public void setMsg(String msg) {
                    this.msg = msg;
                }

                public String getMsg() {
                    return this.msg;
                }

                public void setRes(Res res) {
                    this.res = res;
                }

                public Res getRes() {
                    return this.res;
                }

                public void setCode(int code) {
                    this.code = code;
                }

                public int getCode() {
                    return this.code;
                }
            }

        }
    }
}
